package com.czn.service.impl;

import com.czn.dao.AppointmentDao;
import com.czn.entity.Appointment;
import com.czn.service.IAppointmentService;
import com.czn.vo.QueryVo;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author ChenZeNan
 * @create 2021-04-23 17:19
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentDao appointmentDao;

    /**
     * 分页/模糊查询预约信息
     *
     * @return
     */
    @Override
    public PageQuery<Appointment> findAppointmentList(QueryVo queryVo) {
        PageQuery<Appointment> query = new PageQuery<>(queryVo.getPageNo(), queryVo.getPageSize());
        if (!StringUtils.isEmpty(queryVo.getPatientName())) {
            query.setPara("patientName", '%' + queryVo.getPatientName() + '%');
        }
        if (!StringUtils.isEmpty(queryVo.getDoctorName())) {
            query.setPara("doctorName", '%' + queryVo.getDoctorName() + '%');
        }
        if (queryVo.getUserId() != null) {
            query.setPara("userId", queryVo.getUserId());
        }
        appointmentDao.selectLit(query);
        return query;
    }

    /**
     * 根据医生查询所有预约信息
     *
     * @param queryVo
     * @return
     */
    @Override
    public PageQuery<Appointment> findAppointmentListByDoctor(QueryVo queryVo) {
        PageQuery<Appointment> query = new PageQuery<>(queryVo.getPageNo(), queryVo.getPageSize());
        if (!StringUtils.isEmpty(queryVo.getPatientName())) {
            query.setPara("patientName", '%' + queryVo.getPatientName() + '%');
        }
        if (!StringUtils.isEmpty(queryVo.getTime())) {
            query.setPara("Time", queryVo.getTime());
        }
        if (queryVo.getUserId() != null) {
            query.setPara("userId", queryVo.getUserId());
        }
        appointmentDao.selectListByDoctor(query);
        return query;
    }

    /**
     * 新增预约记录
     *
     * @param appointment
     * @return
     */
    @Override
    public ResponseResult addAppointment(Appointment appointment) {
        ResponseResult result = new ResponseResult();
        appointmentDao.insertReturnKey(appointment);
        result.setCode("300");
        result.setMessage("信息添加成功！");
        return result;
    }

    /**
     * 编辑预约记录
     *
     * @param appointment
     * @return
     */
    @Override
    public ResponseResult editAppointment(Appointment appointment) {
        ResponseResult result = new ResponseResult();
        appointmentDao.updateTemplateById(appointment);
        result.setCode("500");
        result.setMessage("信息更新成功！");
        return result;
    }

    /**
     * 根据预约记录的id查询预约信息
     *
     * @param id
     * @return
     */
    @Override
    public Appointment findAppointmentById(Integer id) {
        Appointment appointment = appointmentDao.selectOne(id);
        return appointment;
    }

    /**
     * 根据id删除预约记录
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteAppointmentById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = appointmentDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败！");
        return result;
    }

    /**
     * 新增预约记录并返回记录的id
     *
     * @param appointment
     * @return
     */
    @Override
    public Integer addAppointmentReturnId(Appointment appointment) {
        int appointmentId = appointmentDao.insertReturnKey(appointment).getInt();
        return appointmentId;
    }

}
